package util;

import sun.security.provider.MD5;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author spencercjh
 */
public class CommonUtil {
    /**
     * read byte[] from inputFile
     */
    public static byte[] suckFile(String inputfile) throws IOException {
        InputStream is = new FileInputStream(inputfile);
        int size = is.available();
        byte[] content = new byte[size];
        is.read(content);
        is.close();
        return content;
    }

    /**
     * write byte[] into outputfile
     */
    public static void spitFile(String outputfile, byte[] b) throws IOException {
        OutputStream os = new FileOutputStream(outputfile);
        os.write(b);
        os.close();
    }

    /**
     * 功能：Java读取txt文件的内容
     * 步骤：1：先获得文件句柄
     * 2：获得文件句柄当做是输入一个字节码流，需要对这个输入流进行读取
     * 3：读取到输入流后，需要读取生成字节流
     * 4：一行一行的输出。readline()。
     * 备注：需要考虑的是异常情况
     */
    private static List<String> readTxtFile(String filePath, String charsetName) {
        List<String> list = new ArrayList<>();
        try {
            File file = new File(filePath);
            /*判断文件是否存在*/
            if (file.isFile() && file.exists()) {
                /*考虑到编码格式*/
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), charsetName);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    list.add(lineTxt);
                }
                read.close();
            } else {
                System.err.println("文件判断相同时找不到指定的文件");
            }
        } catch (Exception e) {
            System.err.println("文件判断相同时读取文件内容出错");
            e.printStackTrace();
        }
        return list;

    }

    @SuppressWarnings("SuspiciousMethodCalls")
    public static boolean issame(String filePath1, String filePath2, String charsetName) {
        List<String> list = readTxtFile(filePath1, charsetName);
        List<String> list2 = readTxtFile(filePath2, charsetName);
        for (Object aList : list) {
            if (!list2.contains(aList)) {
                return false;
            }
        }
        return true;
    }

    /**
     * MD5摘要算法判断密文内容是否一致
     *
     * @param filePath1
     * @param filePath2
     * @return boolean
     */
    public static boolean issame(String filePath1, String filePath2) {
        try {
            byte[] file1 = CommonUtil.suckFile(filePath1);
            byte[] file2 = CommonUtil.suckFile(filePath2);
            file1 = MD5Util.md5(file1);
            file2 = MD5Util.md5(file2);
            assert file1 != null;
            assert file2 != null;
            if (Arrays.equals(file1, file2)) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("判断文件一致时读取密文失败");
        }
        return false;
    }
//    public static boolean issame(String )
}
